﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Libs.Db;

namespace Libs.HR
{
    public class FileData
    {
        public int FileID { get; set; }
        public byte[] Data { get; set; }
        public string FileType { get; set; }
        public string FileName { get; set; }
        public int Size { get; set; }
        public DateTime LogTime { get; set; }
        public int ReturnValue { get; set; }

        public FileData()
        {

        }

        ~FileData()
        {

        }

        public virtual void Dispose()
        {

        }

        /// <summary>
        /// Lấy giá trị FileData theo ID
        /// </summary>
        /// <param name="fileID"></param>
        /// <returns></returns>
        public FileData Get(int fileID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<FileData>("sp_FileData_Select",
                new SqlParameter("@FileID", fileID));
        }

        /// <summary>
        /// Lấy giá trị FileData theo ID
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public FileData Get()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<FileData>("sp_FileData_Select",
                new SqlParameter("@FileID", FileID));
        }

        /// <summary>
        /// Thêm mới FileData
        /// </summary>
        public void Add()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[5];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@Data", Data);
            pars[2] = new SqlParameter("@FileType", FileType);
            pars[3] = new SqlParameter("@FileName", FileName);
            pars[4] = new SqlParameter("@Size", Size);

            db.ExecuteNonQuerySP("sp_FileData_Insert", pars);
            FileID = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Cập nhật FileData
        /// </summary>
        public void Update()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[6];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@FileID", FileID);
            pars[2] = new SqlParameter("@Data", Data);
            pars[3] = new SqlParameter("@FileType", FileType);
            pars[4] = new SqlParameter("@FileName", FileName);
            pars[5] = new SqlParameter("@Size", Size);

            db.ExecuteNonQuerySP("sp_FileData_Update", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Xoá fileID
        /// </summary>
        /// <param name="typeID"></param>
        public void Delete(int fileID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_FileData_Delete",
                new SqlParameter("@FileID", fileID));
        }

        /// <summary>
        /// Xoá Contract
        /// </summary>
        public void Delete()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_FileData_Delete",
                new SqlParameter("@FileID", FileID));
        }

    }
}
